package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class RotationActivity extends Activity {
	public static String analysisorigin = "";
	
	ImageView ivImage;
	Button btRotateleft, btRotateright, btSave, btBlur, btPrev, btAuto;
	public static Bitmap originalBmp; //origin
	public static Bitmap activeBmp; //edited image
	public static Bitmap colorBmp;
	
	//for auto activity
	public static Settings settings;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rotationactivity);
		
		if (getIntent().hasExtra("analysisorigin")) {
			analysisorigin = getIntent().getStringExtra("analysisorigin");
		}
		
		ivImage = (ImageView)findViewById(R.id.ivImage);
				
		
		byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		activeBmp = null;
		originalBmp = null;
		colorBmp = null;
		
		//originalBmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		try {
			originalBmp = InternalStorage.readBitmap(this, "originalBmp");
			
			//Bitmap resizedbmp = Bitmap.createScaledBitmap(originalBmp, 300,
			//		300, true);
			ivImage.setImageBitmap(originalBmp);
			
			//color image
			//byte[] colorbyteArray = getIntent().getByteArrayExtra("colorBmp");
			//colorBmp = BitmapFactory.decodeByteArray(colorbyteArray, 0, colorbyteArray.length);
			//colorBmp = BrightnessActivity2.colorBmp;
			colorBmp = InternalStorage.readBitmap(this, "colorBmp");
			
			btRotateleft = (Button)findViewById(R.id.btRotateleft);
			btRotateright = (Button)findViewById(R.id.btRotateright);
			btSave = (Button)findViewById(R.id.btSave);
			btBlur = (Button)findViewById(R.id.btBlur);
			btPrev = (Button)findViewById(R.id.btPrev);
			//7/8
			btAuto = (Button)findViewById(R.id.btAuto);
			btAuto.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					SettingsDialog sd = new SettingsDialog(RotationActivity.this);
					sd.show();
					
				}
			});
			
			btRotateleft.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(activeBmp == null){
						Bitmap rotatedbmp = RotateBitmap(originalBmp, -90);
						activeBmp = rotatedbmp;
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						colorBmp = RotateBitmap(colorBmp, -90);
					}
					else
					{
						Bitmap rotatedbmp = RotateBitmap(activeBmp, -90);
						activeBmp = rotatedbmp;
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						colorBmp = RotateBitmap(colorBmp, -90);
					}
					
				}
			});
			
			btRotateright.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(activeBmp == null){
						Bitmap rotatedbmp = RotateBitmap(originalBmp, 90);
						activeBmp = rotatedbmp;
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						colorBmp = RotateBitmap(colorBmp, 90);
						
					}
					else
					{
						Bitmap rotatedbmp = RotateBitmap(activeBmp, 90);
						activeBmp = rotatedbmp;
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						colorBmp = RotateBitmap(colorBmp, 90);
					}
					
				}
			});
			
			btBlur.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent goToNextActivity = new Intent(getApplicationContext(),
							GaussianActivity.class);
					goToNextActivity.putExtra("analysisorigin", analysisorigin);
					/*byte[] bytes;
					if
					(activeBmp == null){
						bytes = compressImage(originalBmp);
					}
					else
					{
						bytes = compressImage(activeBmp);
					}
					goToNextActivity.putExtra("BMP", bytes);
					*/
					
					if(activeBmp == null){
						try {
							InternalStorage.writeBitmap(RotationActivity.this, "rotatedBmp", originalBmp);

							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						try {
							InternalStorage.writeBitmap(RotationActivity.this, "rotatedBmp", activeBmp);
							//InternalStorage.writeBitmap(RotationActivity.this, "colorBmp", colorBmp);
							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					
				}
			});
			
			btPrev.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//if is brightnessactivity2 then change here
					startActivity(new Intent(RotationActivity.this, BrightnessActivity3.class));
				}
			});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static Bitmap RotateBitmap(Bitmap source, float angle) {
		Matrix matrix = new Matrix();
		matrix.postRotate(angle);
		return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
				source.getHeight(), matrix, true);
	}
	
	public byte[] compressImage(Bitmap image)
	{
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 100, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;
		
	}
}
